#include "esp_common.h"
#include "gpio.h"
#include "spi.h"

#define BNRG_SELECT() 	GPIO_OUTPUT(SPI_CS_PIN, 0)
#define BNRG_DESELECT()  GPIO_OUTPUT(SPI_CS_PIN, 1)

enum bluenrg_status_t{
    BLUENRG_OK = 0x00,
    BLUENRG_FAILED,
};

uint8_t bluenrg_up(void)
{
    /*bluenrg reset*/
    RST_L;
	vTaskDelay(10 / portTICK_RATE_MS);
	RST_H;
	vTaskDelay(10 / portTICK_RATE_MS);
	#if 1
	while(!(gpio_input_get() & BLE_IRQ_PIN)){

	};
    #endif
    
    return BLUENRG_OK;
    
}

uint8_t bluenrg_spi_write(uint8_t type, uint8_t* data, uint8_t len)
{
    uint8_t header_master[5] = {0x0a, 0x00, 0x00, 0x00, 0x00};
    uint8_t header_slave[5] ;
    uint8_t i, retry;
    uint8_t ready;
    uint16_t wbuf;
    uint8_t status;

    status = 1;
    for (retry=0; retry<100; retry++) {
        BNRG_SELECT();
        /* read SPI header */  
        for(i = 0; i < 5; i++){
            //header_slave[i] = spi_exchange(BNRG_SPI, header_master[i]);
            header_slave[i] = spi_send_byte(header_master[i]);
        }
        printf("header : %x\n", header_slave[0]);
        ready = header_slave[0];
        wbuf = header_slave[1] | (header_slave[2] << 8);
        if ((ready == 0x02) &&
            (wbuf >= len)) {
            // slave is ready to receive
            //spi_exchange(BNRG_SPI,type);
            spi_send_byte(type);
            for(i = 0; i < len; i++){
                //spi_exchange(BNRG_SPI,data[i]);
                spi_send_byte(data[i]);
            }
            status = 0;
            BNRG_DESELECT();
            break;
        }
        
        /* Release CS line */
        BNRG_DESELECT();
        //Delay1us(30);
    }

    return status;
}

uint8_t bluenrg_spi_read(uint8_t* data, uint8_t* plen)
{
    uint8_t i, status;
    uint16_t byte_count;
    uint8_t char_ff = 0xff;
    volatile uint8_t read_char;
    uint8_t header_master[5] = {0x0b, 0x00, 0x00, 0x00, 0x00};
    uint8_t header_slave[5] = {0x00,0x00, 0x00, 0x00, 0x00};

    /* CS reset */
    BNRG_SELECT();
    /* Read the header */  
    for(i = 0; i < 5; i++){
        //header_slave[i] = spi_exchange(BNRG_SPI, header_master[i]);
        header_slave[i] = spi_send_byte(header_master[i]);
        printf("header_slave:%x,",header_slave[i]);
    }
    printf("\n");

    if(header_slave[0] == 0x02) {
        #if 1
        printf("spi_read device is ready\n");
        #endif
        /* device is ready */
        byte_count = (header_slave[4]<<8)|header_slave[3];
  		#if 1
        printf("byte_count:%x\n", byte_count);
  		#endif
        if(byte_count > 0) {
  
            /* avoid to read more data that size of the buffer */
            if(byte_count > *plen){
                byte_count = *plen;
            }
  
            for(i = 0; i < byte_count; i++){ 
                //read_char = spi_exchange(BNRG_SPI, char_ff);
                read_char = spi_send_byte(char_ff);
                data[i] = read_char;
                #if 1
                printf("i: %x, data: %x\n", i, data[i]);
                #endif                                                             
            }                  
        }                                                                       
        *plen = byte_count;
        status = 0;
    }else{
    	#if 1
        printf("spi read device is not ready\n");
        #endif

        *plen = 0;
        status = 1;
   
        goto end;
    }
    end:
    /* Release CS line */
    printf("end \n");
    BNRG_DESELECT();
    //Delay1us(30);
    return status;
}